package crypto;

import java.applet.Applet;
import java.awt.Button;
import java.awt.Font;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CryptoDemo extends Applet implements ActionListener {
	/**
	 * Returns the relative amount the character occurs in the text
	 * @param text The text to look at
	 * @param letter The character to search for
	 * @return The percentage of the text that is said character
	 */
	public static double CharacterPercentage(String text, char letter){
		double count = 0;
		int length = text.length();
		for(int i = 0; i < length; i++){
			if(text.charAt(i) == letter){
				count++;
			}

		}
		return (count/length)*100;
	}
	
	/**
	 * Should only fire when someone presses the analyze button
	 */
	public void actionPerformed(ActionEvent event) {
		String alphabet = "abcdefghijklmnopqrstuvwxyz ";
		if (event.getSource() == ButtonCalculate) {
			// first we'll get the standard values from another file:
			double[] defaults = new DefaultProbability().GetValues();
			double[] textFreq = new double[alphabet.length()];

			String input = FieldToAnalyze.getText();
			String analyzeOutput = "";

			for (int i = 0; i < alphabet.length() && i < 26; i++) {
				char letter = alphabet.charAt(i);
				textFreq[i] = CharacterPercentage(input, letter); 
				analyzeOutput = analyzeOutput + letter + ": " + textFreq[i] + "%" + " - " + defaults[i] + "%\n";
			}

			FieldResult.setText(analyzeOutput);
		}
		if (event.getSource() == ButtonSwap) {
			char from = FieldFrom.getText().charAt(0);
			char to = FieldTo.getText().charAt(0);
			String input = FieldToAnalyze.getText();
			String decryptOutput = "";
			for (int i = 0; i < input.length(); i++) {
				char letter = input.charAt(i);
				if (letter == from) {
					decryptOutput = decryptOutput + to;
				} else {
					decryptOutput = decryptOutput + letter;
				}
			}
			FieldToAnalyze.setText(decryptOutput);
		}
		if (event.getSource() == ButtonEstimate) {
			double[] defaults = new DefaultProbability().GetValues();
			double[] textFreq = new double[alphabet.length()];

			String input = FieldToAnalyze.getText();
			
			char letterFrom = FieldFrom.getText().charAt(0);
			int letterNum = letterFrom - 'a';

			for (int i = 0; i < alphabet.length() && i < 26; i++) {
				char letter = alphabet.charAt(i);
				textFreq[i] = CharacterPercentage(input, letter); 
			}

			double bestFreq = 100;
			int bestChar = 0;
			for (int i = 0; i < alphabet.length() && i < 26; i++) {
				double relFreq = defaults[i] / textFreq[letterNum];
				if ((relFreq < 1 && bestFreq > 1 / relFreq) || (relFreq > 1 && bestFreq > relFreq) || relFreq == 1) {
					bestFreq = (relFreq < 1) ? (1 / relFreq) : relFreq;
					bestChar = i;
				}
			}
			FieldTo.setText("" + alphabet.charAt(bestChar));
		}
	}

	/**
	 * To appease Eclipse, here is a serial version.
	 */
	private static final long serialVersionUID = 2735875257977762284L;
	
	/**
	 * The elements of the first applet
	 */
	private TextField FieldToAnalyze = new TextField("deze zin bevat een hoeveelheid e's die lijkt op de standaard");
	private TextField FieldResult = new TextField("Hier krijg je een lijst met letters en hun frequentie", 26);
	private Button ButtonCalculate = new Button("Analyseer tekst!");
	private TextField FieldFrom = new TextField();
	private TextField FieldTo = new TextField();
	private Button ButtonSwap = new Button("Vervang letters!");
	private Button ButtonEstimate = new Button("Bereken vervanging!");

	/**
	 * Initialize the first applet, I guess.
	 */
	public void init() {
		setFont(new Font("Courier New", Font.PLAIN, 12));
		setSize(800, 600);
		
		Panel cp = new Panel(null); // is het wel een goed idee om een deel van je venster in een functie te definieren die later ook eindigt zonder dat het window sluit en dus dat je daarom ook iets mist?
		// pech we doen het toch
		cp.setBounds(0, 0, 800, 600);
		add(cp);

		// maak een flitsend scherm! woooooo
		FieldToAnalyze.setBounds(24, 24, 752, 18);
		cp.add(FieldToAnalyze);
		FieldResult.setBounds(24, 64, 400, 520);
		cp.add(FieldResult);
		
		FieldFrom.setBounds(460, 102, 50, 18);
		cp.add(FieldFrom);
		FieldTo.setBounds(460, 132, 50, 18);
		cp.add(FieldTo);
		
		ButtonCalculate.setBounds(460, 64, 200, 24);
		ButtonCalculate.addActionListener(this);
		cp.add(ButtonCalculate);
		
		ButtonSwap.setBounds(460, 162, 200, 24);
		ButtonSwap.addActionListener(this);
		cp.add(ButtonSwap);
		
		ButtonEstimate.setBounds(460, 192, 200, 24);
		ButtonEstimate.addActionListener(this);
		cp.add(ButtonEstimate);
	}
}
